package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.MyScrollView;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;


/**
 * Created by yinzhiqun on 2015/4/28.
 * 设置体重对话框
 */
public class SetHeightDialog extends Dialog {
    private TextView resultTv;
    private float cell;
    private int currentHeight;
    private CommitDialogCallBack callback;
    private MyScrollView height_sv;
    private TextView height_number_tv;
    private String watchName;
    private Context context;

    /**
     * @param resultTv 修改完成后，显示结果的textview
     */
    public SetHeightDialog(Context context, int currentHeight, TextView resultTv, CommitDialogCallBack callback) {
        super(context);
        this.resultTv = resultTv;
        this.currentHeight = currentHeight;
        this.callback = callback;
    }

    public SetHeightDialog(Context context, int currentHeight, TextView resultTv, String watchName, CommitDialogCallBack callback) {
        super(context);
        this.resultTv = resultTv;
        this.currentHeight = currentHeight;
        this.callback = callback;
        this.watchName = watchName;
        this.context = context;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_setheight);


        setDialogAttributes();
        initDialogView();
    }

    private void initDialogView() {
        cell = transfer(7);//每格是相对7个单位

        height_number_tv = (TextView) findViewById(R.id.height_number_tv);
        height_number_tv.setText(String.valueOf(currentHeight));

        View height_iv = findViewById(R.id.height_iv);
        int heightIvPadding = DensityUtil.dip2px(getContext(), 51) - (int) (cell * 5 + 0.5f);
        height_iv.setPadding(0, heightIvPadding, 0, heightIvPadding);

        height_sv = (MyScrollView) findViewById(R.id.height_sv);
        height_sv.post(new Runnable() {
            @Override
            public void run() {
                height_sv.scrollTo(0, (int) (cell * (200 - currentHeight) + 0.5f));
                height_sv.setScrollChangedListener(new MyScrollView.ScrollViewListener() {
                    @Override
                    public void onScrollChanged(MyScrollView scrollView, int x, int y,
                                                int oldx, int oldy) {
                        if (y != oldy) {
                            currentHeight = convertToHeight(y);
                            height_number_tv.setText(String.valueOf(currentHeight));
                        }
                    }
                });
            }
        });


        //初始化确定，取消按钮
        findViewById(R.id.cancel_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sure_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    resultTv.setText(currentHeight + "cm");
                    callback.onCommit();
                    dismiss();
                }
                return false;
            }
        });
        if (watchName != null) {
            MyFrontUtil.initTextViewText(context, (TextView) findViewById(R.id.title_name_tv), watchName, context.getString(R.string.t_info_height));
        }
    }


    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    /**
     * 相对单位转绝对像素
     *
     * @param source
     * @return
     */
    private float transfer(float source) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return source * scale;
    }

    private int convertToHeight(int y) {

        return 200 - (int) (y / cell + 0.5f);
    }

}
